package m.jclecai.com.utils;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.view.klpk.PokerToast;

/**
 * Created by andy on 2017/6/9.
 */

public class KlpkUtils {

    public static PokerToast mPokerToast;

    //数字和牌之间的映射关系；
    public static final String[] pokerNumer={"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    //pagerFragment里面是索引对应的牌型；
    public static final String[] indexType={"对子","同花","顺子","同花顺","豹子","任选一","任选二","任选三","任选四","任选五","任选六"};

    //注：这里面的图片的资源id的顺序是和PokerBean里面的枚举的类型的顺序一样的；
    //这里是同花的资源id；
    public static final int [] sameColorRes={R.drawable.same_color_spade,R.drawable.same_color_hearts,R.drawable.same_color_plum,R.drawable.same_color_box};
    //这里是同花顺的资源id；
    public static final int [] straightFlushRes={R.drawable.straight_flush_spade,R.drawable.straight_flush_hearts,R.drawable.straight_flush_plum,R.drawable.straight_flush_box};

    /**
     * pagerFragment下面根据index,和position来显示相应的paixing
     */
    public static final String[][] pokerType={
            {"包选","AA","22","33","44","55","66","77","88","99","1010","JJ","QQ","KK"},
            {"包选","黑桃","红桃","梅花","方块"},
            {"包选","A23","234","345","456","567","678","789","8910","910J","10JQ","JQK","QKA"},
            {"包选","黑桃","红桃","梅花","方块"},
            {"包选","AAA","222","333","444","555","666","777","888","999","101010","JJJ","QQQ","KKK"},
            {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
    };

    public static final int[] pokerHistoryBgRes={
        R.drawable.klpk_history_spade,
        R.drawable.klpk_history_hearts,
        R.drawable.klpk_history_plum,
        R.drawable.klpk_history_box
    };




    public static void showText(Context context,String content){
        if (mPokerToast == null) {
            mPokerToast = new PokerToast(context);
        }
        mPokerToast.showText(content);
    }



    /**
     * 传入字符串，如果解析出问题的话，我们是返回一个Null的，如果没问题的话，我们就将字符串转换为相应的的List集合；
     * @param str
     * @return
     */
    public static List<PokerBean> getPokerBeanList(String  str){

        List<PokerBean> mList= null;
        try {
            mList = new ArrayList<>();
            String[] split = str.split(",");
            for (String s : split) {
                PokerBean pokerBean = new PokerBean();
                String[] split1 = s.split("-");
                //这里将种类和大小都解析出来了
                String category = split1[0];
                String text = split1[1];
                if("1".equals(category)){
                    pokerBean.setCategory(PokerBean.Category.spades);
                }else if("2".equals(category)){
                    pokerBean.setCategory(PokerBean.Category.hearts);
                }else if("3".equals(category)){
                    pokerBean.setCategory(PokerBean.Category.pulm_blossomm);
                }else if("4".equals(category)){
                    pokerBean.setCategory(PokerBean.Category.box);
                }
                if ("A".equals(text)) {
                    pokerBean.setNumber(10);
                }else if("J".equals(text)){
                    pokerBean.setNumber(11);
                }else if("Q".equals(text)){
                    pokerBean.setNumber(12);
                }else if("K".equals(text)){
                    pokerBean.setNumber(13);
                }else{
                    //其他的情况，我们就将其转化为对应的整型
                    pokerBean.setNumber(Integer.parseInt(text));
                }
                mList.add(pokerBean);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return mList;
    }



    /**
     * 判断牌型，这里的话,根据传递进来的List集合我们来判断是什么牌型,
     *
     * @param pokerBeanList
     * @return
     */
    public static String getBrandType(List<PokerBean> pokerBeanList) {
        //// TODO: 2017/6/9 这里的话，最好做一下单元测试
        PokerBean poker1 = pokerBeanList.get(0);
        PokerBean poker2 = pokerBeanList.get(1);
        PokerBean poker3 = pokerBeanList.get(2);
        //每一个牌的大小；
        int number1 = poker1.getNumber();
        int number2 = poker2.getNumber();
        int number3 = poker3.getNumber();
        //获取每一个牌的牌型；
        PokerBean.Category category1 = poker1.getCategory();
        PokerBean.Category category2 = poker2.getCategory();
        PokerBean.Category category3 = poker3.getCategory();


        if (number1 == number2 && number1 == number3) {
            return "豹子";
        } else if (category1 == category2 && category1 == category3) {
            return "同花";
        } else if (number1 == number2 || number1 == number3 || number2 == number3) {
            //上面已经判断过豹子，所以这里是不可能是豹子
            return "对子";
        } else {

            double total = (number1 + number2 + number3) * 1.0;
            double avg = total / 3;
            if (avg == number1 || avg == number2 || avg == number3) {
                //这里的话， 我们继续判断；
                double preNumber = avg - 1;
                if (preNumber == number1 || preNumber == number2 || preNumber == number3) {
                    //这里的话，我们认为是顺子；
                    if (category1 == category2 && category1 == category3) {
                        return "同花顺";
                    } else {
                        return "顺子";
                    }
                }
            }
        }

        return "散牌";
    }

}
